using System.Text.Json;
using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using SE.PO.CInterface.DBProtocols.Rooms;
using SE.PO.CInterface.Web.Clients;
using SE.PO.CInterface.Web.Pages.Form;

namespace SE.PO.CInterface.Web.Pages.DB.Room
{
    public partial class DelRoomFrom
    {
        [Parameter]
        public EventCallback OnSuccess { get; set; }
            
        [Parameter]
        public EventCallback OnFailure { get; set; }
        
        [Parameter]
        public EventCallback OnCancel { get; set; }
        
        public Form<RoomDto>? Form;

        [Parameter] public RoomDto Model { get; set; } = new();

        [Parameter] public List<RoomDto> Models { get; set; } = new();

        private readonly FormItemLayout _formItemLayout = new FormItemLayout
        {
            LabelCol = new ColLayoutParam
            {
                Xs = new EmbeddedProperty {Span = 24},
                Sm = new EmbeddedProperty {Span = 7},
            },

            WrapperCol = new ColLayoutParam
            {
                Xs = new EmbeddedProperty {Span = 24},
                Sm = new EmbeddedProperty {Span = 12},
                Md = new EmbeddedProperty {Span = 10},
            }
        };

        private readonly FormItemLayout _submitFormLayout = new FormItemLayout
        {
            WrapperCol = new ColLayoutParam
            {
                Xs = new EmbeddedProperty {Span = 24, Offset = 0},
                Sm = new EmbeddedProperty {Span = 10, Offset = 7},
            }
        };

        private async Task OnFinish()
        {
            if (Models.Any())
            {
                await Models.ForEachAsync(async r => { await Client.Delete(r.Id); });
            }

            if (!Equals(Model, null))
            {
                await Client.Delete(Model.Id);
            }
            await OnSuccess.InvokeAsync();
        }

        private void OnFinishFailed(EditContext editContext)
        {
            Console.WriteLine($"Failed:{JsonSerializer.Serialize(Model)}");
        }
    }
}